// @formatter:off
/**
 * @file WCSimpleContainer.js
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-05 00:31:35 +0000 (Wed, 05 Jun 2013) $
 *
 * $Id: WCSimpleContainer.js 69 2013-06-05 00:31:35Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA2/includes/WCSimpleContainer.js $
 */
// @formatter:on

wcSimpleContainer = {
	isOpened: false,

	close: function() {
		if(this._closeCallback != null) {
			this._closeCallback();
			this._closeCallback = null;
		}
		if(this._window != null) {
			this._window.remove();
			this._window = null;
		}
		this.isOpened = false;
		this._openObservers = [];
	},
	height: function(pixels) {
		if(pixels) {
			$("#wcSimpleContainer_Window").css({
				height: pixels + "px"
			});
			this.reposition();
		}

		return this._window.height();
	},
	open: function(content, callback) {
		var html = '';
		html += '<div id="wcSimpleContainer_Container">';

		html += '<div id="wcSimpleContainer_Background"></div>';

		html += '<div id="wcSimpleContainer_Window">';
		html += '<div id="wcSimpleContainer_Title"></div>';
		html += '<div id="wcSimpleContainer_Content"></div>';
		html += '</div>';

		html += '</div>';

		this._window = $(html);

		$("body").append(this._window);

		var scWindow = $("#wcSimpleContainer_Content");
		scWindow.html(content.html());

		this._closeCallback = callback;
		this.isOpened = true;

		this.reposition();

		for(var i = 0; i < this._openObservers.length; i++) {
			this._openObservers[i](this);
		}

		if(WCShortCuts) {
			WCShortCuts.reset();
			WCShortCuts.add("ESC", function(key, e) {
				wcSimpleContainer.close();
			});
		}
	},
	openUrl: function(url, callback) {
		var html = $('<div class="wcSimpleContainer"></div>');

		$(document).append(html);

		html.load(url, function(data) {
			wcSimpleContainer.open($("<div>" + data + "</div>"), callback);
		});
	},
	reposition: function() {
		var scWindow = $("#wcSimpleContainer_Window");
		var left = window.pageXOffset + ( (window.innerWidth - scWindow.width()) / 2);
		var top = window.pageYOffset + ( (window.innerHeight - scWindow.height()) / 2);
		scWindow.css({
			left: left,
			top: top
		});

		this._lastHeight = $("#wcSimpleContainer_Content").height();
		this._lastWidth = $("#wcSimpleContainer_Content").width();
	},
	resize: function() {
		var scContent = $("#wcSimpleContainer_Content");
		var scWindow = $("#wcSimpleContainer_Window");
		if(this._lastHeight != scContent.height() || this._lastWidth != scContent.width()) {
			var height = scWindow.height() + (scContent.height() - this._lastHeight);
			var width = scWindow.width() + (scContent.width() - this._lastWidth);

			scWindow.css({
				height: height + "px",
				width: width + "px"
			});
		}

		this.reposition();
	},
	width: function(pixels) {
		if(pixels) {
			$("#wcSimpleContainer_Window").css({
				width: pixels + "px"
			});
			this.reposition();
		}

		return this._window.width();
	},
	title: function(value) {
		if(value) {
			$("#wcSimpleContainer_Title").text(value);
		}

		return $("#wcSimpleContainer_Title").text();
	},
	whenOpened: function(observer) {
		this._openObservers.push(observer);
	},

	_closeCallback: null,
	_lastHeight: 0,
	_lastWidth: 0,
	_openObservers: [],
	_window: null
};
